﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Kinect;
using Microsoft.Samples.Kinect.KinectExplorer;
using Microsoft.Samples.Kinect.WpfViewers;

namespace ZeroTouchUI
{
    /// <summary>
    /// Interaction logic for ProfileSelect.xaml
    /// </summary>
    public partial class ProfileSelect : Window
    {
        private List<Profile> profiles;
        public Microsoft.Samples.Kinect.KinectExplorer.KinectExplorer explorer;
        private readonly Uri expIconUri = new Uri("pack://application:,,,/KinectExplorer.ico", UriKind.RelativeOrAbsolute);

        public ProfileSelect()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, EventArgs e)
        {
            ActivateExplorer();
            List<string> files = (Directory.GetFiles(Directory.GetCurrentDirectory())).ToList<string>();
            profiles = new List<Profile>();
            foreach (string file in files)
            {
                if (file.EndsWith(".xml"))
                {
                    string filename = System.IO.Path.GetFileNameWithoutExtension(file);
                    Profile p = new Profile();
                    try
                    {
                        p.LoadProfile(filename, p);
                        if (p.getDictionary().Count > 0)
                        {
                            profileSelect.Items.Add(new ComboBoxItem { Content = filename });
                            profiles.Add(p);
                        }
                    }
                    catch
                    {
                    }
                }
            }

            cover.Visibility = System.Windows.Visibility.Hidden;
            LoadingText.Visibility = System.Windows.Visibility.Hidden;
            nsaImage.Visibility = System.Windows.Visibility.Hidden;
        }

        private void ActivateExplorer()
        {
            if (explorer == null)
            {
                explorer = new Microsoft.Samples.Kinect.KinectExplorer.KinectExplorer
                {
                    Icon = BitmapFrame.Create(expIconUri),
                    Visibility = System.Windows.Visibility.Hidden,
                    WindowState = System.Windows.WindowState.Minimized
                };
                explorer.Closed += new EventHandler(Explorer_Closed);
                explorer.Show();
            }
        }

        private void newProfileRadio_Checked(object sender, RoutedEventArgs e)
        {
            this.goButton.IsEnabled = newProfileName.Text.Count() > 0;
            this.newRow.Height = new GridLength(20);
        }

        private void newProfileRadio_Unchecked(object sender, RoutedEventArgs e)
        {
            this.newRow.Height = new GridLength(0);
        }

        private void loadProfileRadio_Checked(object sender, RoutedEventArgs e)
        {
            this.goButton.IsEnabled = profileSelect.SelectedIndex >= 0;
            this.existingRow.Height = new GridLength(20);
        }

        private void loadProfileRadio_Unchecked(object sender, RoutedEventArgs e)
        {
            this.existingRow.Height = new GridLength(0);
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.goButton.IsEnabled = newProfileName.Text.Count() > 0;
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.goButton.IsEnabled = profileSelect.SelectedIndex >= 0;
        }

        private void goButton_Click(object sender, RoutedEventArgs e)
        {
            NewMainWindow main;
            if (this.newProfileRadio.IsChecked == true && this.newProfileName.Text.Count() > 0)
            {
                main = new NewMainWindow { Profile = new Profile { Name = this.newProfileName.Text }, explorer = this.explorer };
            }
            else if (this.loadProfileRadio.IsChecked == true && this.profileSelect.SelectedIndex >= 0)
            {
                main = new NewMainWindow { Profile = profiles[profileSelect.SelectedIndex], explorer = this.explorer };
            }
            else
            {
                return;
            }

            main.Show();
            this.Closed -= this.Window_Closed;
            this.Close();
        }

        private void exitButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Explorer_Closed(object sender, EventArgs e)
        {
            foreach (KinectSensorItem kinect in explorer.sensorItems)
            {
                if (kinect.Window != null)
                {
                    kinect.Window.Close();
                }
            }

            explorer = null;
        }
    }
}
